import searchElement from '../../components/search'
import tableElement from '../../components/table'
import tabElement from '../../components/tab'
import config from '../../api/config'

export default {
    data() {
        return {
            msg: 'hello this is index',
            activeNames: ['1'],
            tableData: {
                label: [
                    { name: '捐助人昵称', msgname: 'donorname' },
                    { name: '项目名称', msgname: 'pi_name' },
                    { name: '订单编号', msgname: 'utl_order_number' },
                    { name: '捐助的金额', msgname: 'utl_donation_amount' },
                    { name: '捐助的寄语', msgname: 'utl_greeting_message' },
                    { name: '邀请人的昵称', msgname: 'inventername' },
                    { name: '捐助时间', msgname: 'created_at' },
                    {
                        name: '捐助品', msgname: 'pdt_name', filter: function (val) {
                            if (val) {
                                return val
                            } else {
                                return '其他'
                            }
                        }
                    },

                ],
                data: [],
                opate: '',
                all: ''
            },
            img: false,
            dialogFormVisible: false,
            formLabelWidth: '150px',
            form: {
                a_title: '',
                a_content: '',
                a_type: '',
                a_isshow: ''
            },
            formtitle: '新增信息',
            rules: {
                a_title: [
                    { required: true, message: '请输入文章标题', trigger: 'blur' },
                ],
                a_content: [
                    { required: true, message: '请输入文章内容', trigger: 'blur' }
                ],
                a_type: [
                    { required: true, message: '请选择类型', trigger: 'change' },
                ],
                a_isshow: [
                    { required: true, message: '请选择是否显示', trigger: 'change' },
                ],
            },
            delVal: [],
            pageTotal: 0,
            page: {
                page: 1,
                page_limit: 10
            },
            pageSizes: [],
            searchData: {
                ui_nickname: '',
                utl_order_number: ''
            }
        }
    },
    methods: {
        __msg(data) {
            this.$alert('用户信息有误，请重新登陆', data, {
                confirmButtonText: '确定',
                callback: action => {
                    this.$router.push({ path: '/login' });
                }
            });
        },
        __error(data) {
            switch (data) {
                case 401:
                    this.__msg(data);
                    break;
                case -404:
                    this.tableData.data = [];
                    this.pageTotal = 0;
                    break;
                default:
                    break;
            }
        },
        handleSizeChange(size) {
            this.page.page_limit = size;
            this.getList(this.page)
        },
        changePage(page) {
            this.page.page = page;
            this.getList(this.page)
        },
        acquire(data) {
            if (data.opate == 'edit') {
                this.edit(data.data)
            } else if (data.opate == 'msg') {
                this.delVal = data.data
            } else if (data.opate == 'detail') {

            }
        },
        // 搜索
        onSubmit() {
            this.searchData.page = this.page.page
            this.searchData.page_limit = this.page.page_limit
            this.getList(this.searchData)

        },
        // 获取列表
        getList(data) {
            config.__order.List(data).then((req) => {
                if (req.data.code === 0) {
                    this.tableData.data = req.data.data.data;
                    this.pageTotal = req.data.data.total;
                    let num = Math.ceil(parseInt(this.pageTotal, 10) / 10)
                    if (this.pageSizes.length < 1) {
                        for (var index = 0; index < num; index++) {
                            this.pageSizes.push((index + 1) * 10)
                        }
                    }
                } else {
                    this.__error(req.data.code);
                }
            }).catch((error) => {
                console.log(error.req)
                this.__error(error.response.status);
            })
        }
    },
    components: { searchElement, tableElement, tabElement },
    mounted() {
        this.$nextTick(function () {
            let data = {
                page: 1,
                page_limit: 10
            }
            this.getList(data)
        })
    }
}